package utils;

import book.Book;
import com.bit.utils.FileUtils;
import constant.Constant;

import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 19182
 * Date: 2025-07-04
 * Time: 19:16
 */
//让图书数据存储在磁盘文件中，数据具有持久化的类
public class AnalyzingBook {
    //books数组是每本书的集合，也就是说他的每个元素是一个图书对象
    public void storeObject(Book[] books, String filename) throws IOException { //把图书的信息全部存储在文件中，让数据可持久化，不会丢失数据
        int booksUseLen = 0;    //有效的书籍个数
        for (int i = 0; i < books.length ; i++) {
            if(books[i] != null){
                booksUseLen++;
            }
        }
        StringBuilder jsonArray = new StringBuilder();  //StringBuilder可以修改字符串的内容，也就是这个对象可以修改，但是String不行
        for(int i = 0; i < booksUseLen; i++){
            if(books[i] != null){
                jsonArray.append(books[i].toJson());
                if(i != booksUseLen - 1){
                    jsonArray.append("\n"); //一本书籍转换成字符串添加上\n, 方便我们后面从文件读这个数据的时候分割字符串
                }
            }
        }
        FileUtils.writeFile(jsonArray.toString(), filename); //被jsonArray字符串存储的数据存储在磁盘上，数据就可持久化了

    }
    public Book[] loadObject(String filename) throws IOException{   //这里声明一下读写的异常，因为我们读写文件可能失败
        //从文件中读取数据存入到Book数组中
        String content = FileUtils.readFile(filename); //读入到字符串中了
        if(content == null || content.isEmpty()){
            System.out.println("图书馆为空！！！！");
            return null;
        }
        String[] bookJsonStrings = content.split("\n"); //我们度出来的字符串以\n分割存储到这个字符串数组中
        Book[] bookList = new Book[bookJsonStrings.length]; //把对应的字符串组装成图书对象, 这里我们开现在有效数据的空间
        for (int i = 0; i < bookJsonStrings.length; i++) {
            Book book = parseBookJson(bookJsonStrings[i]);
            bookList[i] = book;
        }
        return bookList;
    }

    private Book parseBookJson(String json) {
        String[] pairs = json.split(",");   //以逗号分割每个图书对象中的元素, 如图书ID，书名分割成两个元素
        int bookId = Integer.parseInt(pairs[0]); //包装类的方法把字符串转换成整数
        String title = pairs[1];
        String author = pairs[2];
        String category = pairs[3];
        int publishYear = Integer.parseInt(pairs[4]);
        boolean isBorrowed = Boolean.parseBoolean(pairs[5]);
        int borrowCount = Integer.parseInt(pairs[6]);
        LocalDate shelfDate = LocalDate.parse(pairs[7]);
        if (title != null && author != null && category != null && shelfDate != null) {
            Book book = new Book(title, author, category, publishYear, shelfDate);
            book.setBookId(bookId);
            book.setBorrowed(isBorrowed);
            book.setBorrowedCount(borrowCount);
            return book;
        }
        return null;
    }

    public static void main1(String[] args) {   //测试写入和读取文件方法
        Book[] books = new Book[4];
        books[0] = new Book("java", "gaobo", "编程", 1994, LocalDate.of(2024, 1, 2));
        books[1] = new Book("mysql", "lisi", "编程", 1999, LocalDate.of(2024, 2, 10));
        books[2] = new Book("php", "gaobo", "编程", 2020, LocalDate.of(2023, 9, 23));
        books[3] = new Book("西游记", "吴承恩", "小说", 2024, LocalDate.of(2023, 9, 23));
        AnalyzingBook analyzingBook = new AnalyzingBook();
        try{
            analyzingBook.storeObject(books, Constant.ALL_BOOK_FILE_NAME); //定义成常量解耦，方便修改名字
            Book[] ret = analyzingBook.loadObject(Constant.ALL_BOOK_FILE_NAME);
            for (int i = 0; i < ret.length; i++) {
                System.out.println(ret[i]);
            }

        }catch (IOException e){
            e.printStackTrace();
        }
    }
}
